//
//  StarRate.m
//  iStudy
//
//  Created by LiTengFei on 15/1/20.
//  Copyright (c) 2015年 HugeWing Co.Ltd. All rights reserved.
//

#import "StarRate.h"


@implementation StarRate

@synthesize default_space_width;
@synthesize default_space_height;

@synthesize default_between_space;

@synthesize star_width;
@synthesize star_height;

- (void)commonPare:(CGRect)frame {
    default_space_width = 4;
    star_height = 15;
    star_width = 15;
    default_space_height = (CGRectGetHeight(frame) - star_height ) / 2 ;
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.starImage = [UIImage imageNamed:@"1"];
        self.startHeightImage = [UIImage imageNamed:@"2"];

        [self commonPare:frame];
        [self commonInstance];

        self.maxRate = 5;
        self.rate = 0;

    }
    return self;
}

-(void)commonInstance{
    for (int i=0; i<5; i++) {
        UIImageView * star = [[UIImageView alloc]initWithFrame:CGRectMake(default_space_width +  (star_width +default_between_space)* i  , default_space_height, star_width, star_height)];
        star.tag = i + 1;
        [self addSubview:star];
    }
}

-(void)setRate:(NSInteger)rate{
    _rate = rate;
    for (int i=1; i<=self.maxRate; i++) {
        UIImageView * imageView = (UIImageView *)[self viewWithTag:i];
        if(i <= rate){
            imageView.image = self.starImage;
        }else{
            imageView.image = self.startHeightImage;
        }
    }
}
-(void)setMaxRate:(NSInteger)maxRate{
    _maxRate = maxRate;
    for (int i=1; i<=self.maxRate; i++) {
        UIImageView * imageView = (UIImageView *)[self viewWithTag:i];
        imageView.image = self.startHeightImage;
    }
}
@end
